package com.ysu.shop.controller;

import java.util.Map;
import javax.annotation.Resource;

import com.ysu.shop.service.IGoodsService;
import com.ysu.shop.pojo.Goods;

import com.ysu.shop.utils.Result;
import com.ysu.shop.utils.InitUtil;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;


import lombok.extern.slf4j.Slf4j;
/**
* <p>
    *  前端控制器
    * </p>
*
* @author 耿志龙
* @since 2020-10-01
*/
@RestController
@Slf4j
@RequestMapping("/goods")
public class GoodsController {

    @Value("${server.port}")
    private String port;

    @Resource
    private IGoodsService iGoodsService;

    /**
    * 进行分页查询
    */
    @GetMapping
    public Result getList(@RequestParam Map<String, Object> param) {
        InitUtil.initPage(param);
        int num = Integer.parseInt(param.get("page").toString());
        int limit = Integer.parseInt(param.get("limit").toString());
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        InitUtil.initEq(param, wrapper, "active","user_id");
        IPage<Goods> page = new Page<>(num, limit);
        return Result.getSuccessResult(iGoodsService.page(page, wrapper));
    }

    /**
    * 通过主键id在表中查找信息
    */
    @GetMapping("/{id}")
    public Result getById(@PathVariable int id) {
        Goods iGoods = iGoodsService.getById(id);
        if (iGoods == null) {
            return Result.getFailResult("","无记录");
        }
        return Result.getSuccessResult(iGoods);
    }

    /**
    * name : 需要检查那个字段的value是否在数据库存在
    * 比如/check/RegistworkName?value=阿司匹林  ：检查数据库是否存在RegistworkName='阿司匹林'
    */
    @GetMapping("/check/{name}")
    public Result checkName(@PathVariable String name,@RequestParam String value){
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.eq(name, value);
        if (iGoodsService.getOne(wrapper) != null){
        return Result.getFailResult();
        }
        return Result.getSuccessResult();
    }

    /**
    * 向表中添加一条记录
    */
    @PostMapping()
    public Result save(@RequestBody Goods iGoods){
        if (iGoodsService.save(iGoods)){
            return Result.getSuccessResult("","添加成功");
        }
        return Result.getFailResult("","添加失败");
    }

    /**
    * 根据表中的主键修改 根据表中字段相应的修改，如果存在则修改
    */
    @PutMapping("/{id}")
    public Result update(@RequestBody Goods iGoods, @PathVariable int id) {
        iGoods.setId(id);
        if (iGoodsService.updateById(iGoods)){
        return Result.getSuccessResult("","修改成功");
        }
        return Result.getFailResult("","修改失败");
    }

    /**
    * active 字段 相当于激活一样 设置字段的有效性 active=1 有效
    */
    @PutMapping("/back/{id}")
    public Result back(@PathVariable int id) {
        Goods iGoods = new Goods();
        iGoods.setId(id);
        iGoods.setActive(1);
        if (iGoodsService.updateById(iGoods)){
            return Result.getSuccessResult("","激活成功");
        }
        return Result.getFailResult("","激活失败");
    }

    /**
    * active 字段 相当于激活一样 设置字段的有效性 active=0 无效 逻辑删除
    */
    @PutMapping("/del/{id}")
    public Result loginDel(@PathVariable int id) {
        Goods iGoods = new Goods();
        iGoods.setId(id);
        iGoods.setActive(0);
        if (iGoodsService.updateById(iGoods)){
            return Result.getSuccessResult("","设置失效成功");
        }
        return Result.getFailResult("","设置失效失败");
    }

    /**
    * 批量设置 active=0
    */
    @PutMapping("/batchDel")
    public Result LoginBatchDel(@RequestParam String ids) {
        Goods iGoods = new Goods();
        iGoods.setActive(0);
        String[] idList = ids.split(",");
        for (String id : idList) {
            iGoods.setId(Integer.parseInt(id));
            iGoodsService.updateById(iGoods);
        }
        return Result.getSuccessResult("","批量失效成功");
    }

    /**
    * 在数据表中真正的删除一条记录
    */
    @DeleteMapping("/{id}")
    public Result delById(@PathVariable int id) {
        if (id<21) {
            return Result.getFailResult("","不能删除系统预留项");
        }
        if (iGoodsService.removeById(id)){
            return Result.getSuccessResult("","删除成功");
        }
        return Result.getFailResult("","删除失败");
    }

}
